__file__ = 'A14.py'
__author__ = 'Jerry Liu'
__date__ = '2016-04-14'

"""
    问题：纯文本文件 student.txt为学生信息, 里面的内容（包括花括号）如下所示：
        {
            "1":["张三",150,120,100],
            "2":["李四",90,99,95],
            "3":["王五",60,66,68]
        }
        请将上述内容写到 student.xls
    workbook相关：
    1、http://www.lfd.uci.edu/~gohlke/pythonlibs/下载 openpyxl-2.3.2-py2.py3-none-any.whl,
        和其他两个支持库 et_xmlfile-1.0.1-py2.py3-none-any.whl  jdcal-1.2-py2.py3-none-any.whl
    2、使用命令pip install 进行安装
"""

from openpyxl.workbook import Workbook
from openpyxl.writer.excel import ExcelWriter

def getdata():
    # 读取文档数据
    f = open('d://student.txt').read()
    # 转换为dict类型
    d = eval(f)
    savefile(d)

def savefile(d):
    # 新建excel
    wb = Workbook()
    ew = ExcelWriter(workbook=wb)
    filepath = 'd://student.xlsx'
    # 第一个sheet
    ws = wb.worksheets[0]
    row = 1
    for k, v in d.items():
        print('%s:%s' % (k, v))
        ws.cell(row=row, column=1, value=k)
        for vi in range(len(v)):
            ws.cell(row=row, column=vi + 1, value=v[vi])
        row += 1

    # 保存
    ew.save(filepath)

if __name__ == '__main__':
    getdata()
